import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:xmshop/app/models/category_model.dart';
import 'package:xmshop/app/models/focus_model.dart';
import 'package:xmshop/app/models/plist_model.dart';
import 'package:xmshop/app/services/httpsClient.dart';

class HomeController extends GetxController {
  //TODO: Implement HomeController
  //顶部浮动导航的切换标注
  RxBool flag = false.obs;
  //ListView对应的控制器
  final ScrollController scrollController = ScrollController();
  RxList<FocusItemModel> swiperList = <FocusItemModel>[].obs;
  RxList<CategoryItemModel> categoryList = <CategoryItemModel>[].obs;
  RxList<FocusItemModel> bestSellingSwiperList = <FocusItemModel>[].obs;
  RxList<PlistItemModel> sellingPlist = <PlistItemModel>[].obs;
  RxList<PlistItemModel> bestPlist = <PlistItemModel>[].obs;
  HttpsClient httpsClient = HttpsClient();

  @override
  void onInit() {
    super.onInit();
    scrollControllerListener();
    getFocusData();
    getSellingData();
    getCategoryData();
    getSellingPlistData();
    getBestPlistData();
  }

  scrollControllerListener() {
    scrollController.addListener(() {
      if (scrollController.position.pixels > 10) {
        if (flag.value == false) {
          print(flag);
          flag.value = true;
          update();
        }
      }
      if (scrollController.position.pixels < 10) {
        if (flag.value == true) {
          print(flag);
          flag.value = false;
          update();
        }
      }
    });
  }

  getFocusData() async {
    //var response = await Dio().get("https://miapp.itying.com/api/focus");
    var response = await httpsClient.get("api/focus");
    if (response != null) {
      print(response.data["result"]);
      var focus = FocusModel.fromJson(response.data);
      swiperList.value = focus.result!;
      update();
    }
  }

  getSellingData() async {
    // var response = await Dio().get(
    //   "https://miapp.itying.com/api/focus?position=2",
    // );
    var response = await httpsClient.get("api/focus?position=2");
    if (response != null) {
      var selling = FocusModel.fromJson(response.data);
      bestSellingSwiperList.value = selling.result!;
      update();
    }
  }

  getCategoryData() async {
    //var response = await Dio().get("https://miapp.itying.com/api/bestCate");
    var response = await httpsClient.get("api/bestCate");
    if (response != null) {
      print(response.data["result"]);
      var focus = CategoryModel.fromJson(response.data);
      categoryList.value = focus.result!;
      update();
    }
  }

  getSellingPlistData() async {
    // var response = await Dio().get(
    //   "https://miapp.itying.com/api/plist?is_hot=1&pageSize=3",
    // );
    var response = await httpsClient.get("api/plist?is_hot=1&pageSize=3");
    if (response != null) {
      var plist = PlistModel.fromJson(response.data);
      sellingPlist.value = plist.result!;
      update();
    }
  }

  getBestPlistData() async {
    // var response = await Dio().get(
    //   "https://miapp.itying.com/api/plist?isbest=1",
    // );
    var response = await httpsClient.get("api/plist?isbest=1");
    if (response != null) {
      var plist = PlistModel.fromJson(response.data);
      bestPlist.value = plist.result!;
      update();
    }
  }

  @override
  void onClose() {
    super.onClose();
  }
}
